package com.example.administrator.basedemos.activity;

import android.text.TextUtils;
import android.widget.Button;

import com.example.administrator.basedemos.R;
import com.example.administrator.basedemos.base.BaseActivity;
import com.example.administrator.basedemos.utils.LogUtil;
import com.jakewharton.rxbinding2.view.RxView;

import butterknife.BindView;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.functions.Consumer;


/**
 * 创建者     ZCL
 * 创建时间   2018/3/19 18:04
 * 描述	      ${基于RX的自定义图片框架}
 * <p>
 * 更新者     $Author$
 * 更新时间   $Date$
 * 更新描述   ${TODO}
 */
public class MyPicBaseRxActivity extends BaseActivity {

    private static final String TAG = "MyPicBaseRxActivity";

    @BindView(R.id.pic_btn)
    Button mSbBtn;

    @Override
    public void initView() {

    }

    @Override
    public void initData() {
        /**
         * 发射缓存数据
         */
        final Observable<Object> memory = Observable.create(new ObservableOnSubscribe<Object>() {
            @Override
            public void subscribe(ObservableEmitter<Object> emitter) throws Exception {
                String value = "mm";
                if(TextUtils.isEmpty(value))
                emitter.onComplete();
                    else
                emitter.onNext(value);
            }
        });

        final Observable<Object> disk = Observable.create(new ObservableOnSubscribe<Object>() {
            @Override
            public void subscribe(ObservableEmitter<Object> emitter) throws Exception {
                String value = "disk";
                if(TextUtils.isEmpty(value))
                    emitter.onComplete();
                else
                    emitter.onNext(value);
            }
        });
        final Observable<Object> network = Observable.create(new ObservableOnSubscribe<Object>() {
            @Override
            public void subscribe(ObservableEmitter<Object> emitter) throws Exception {
                String value = "network";
                if(TextUtils.isEmpty(value))
                    emitter.onComplete();
                else
                    emitter.onNext(value);
            }
        });

        RxView.clicks(mSbBtn).subscribe(new Consumer<Object>() {
            @Override
            public void accept(Object o) throws Exception {
                /**
                 而concat操作符是接收若干个Observables，发射数据是有序的，不会交叉。
                 concatMap和flatMap最大的区别是concatMap发射的数据集是有序的，flatMap发射的数据集是无序的。
                 */
                Observable.concat(memory, disk, network)
                        .subscribe(new Consumer<Object>() {
                            @Override
                            public void accept(Object o) throws Exception {
                                LogUtil.util(TAG, "图片来源" + o);
                            }
                        });
            }
        });
    }

    @Override
    public int setLayoutId() {

        return R.layout.activity_base_pic;
    }
}